<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
namespace Live\Controller;
use Think\Controller;

class IndexController extends BaseController {

	/*
     * 直播列表显示
     */
	public function i(){
		$this->display();
	}
	public function i1(){
		$this->display();
	}
	public function i2(){
		$this->display();
	}

    /*
     * 直播列表显示
     */
    public function index(){
    	$this->livelist();
    	$this->display();
    }

	/*
	 * 直播详情页
	 */
	public function liveitem(){
		$liveID = I('get.liveID');
		switch ($liveID){
			case 1:
				$domainid = 919;
				break;
			case 2:
				$domainid = 838;
				break;
			case 3:
				$domainid = 838;
				break;
		}
		$isCheckliveID = preg_match('/^\d*$/',$liveID);
		if($isCheckliveID){

			$res = S('live_'.$liveID);
			if(!$res){
				$liveing = D(C('TABLE_LIVEING'));
				$res = $liveing->find($liveID);
				S('live_'.$liveID,$res);
			}

			if($res){
				$this->tj($liveID); //执行统计功能
				$live_starttime = $res['live_startdate'];
				$live_endtime = $res['live_enddate'];
				$this->autoFiltrateRemind($liveID,$live_starttime,$live_endtime);		//处理过期的直播提醒
				$this->checkPaytoTicket($liveID,$res['live_pre'],$res['live_dirll']);	//判断是否购买过
				$this->checkUsertokenToRemind($liveID);									//判断是否设置过提醒
				$this->checkLiveTimeStatus($live_starttime,$live_endtime);				//直播状态
				$this->checkLiveMoneyStatus($res['live_pre'],$res['live_dirll']);		//判断是免费还是收费
				$this->giveGift();														//显示礼物
				$this->livelist();
				$this->assign('shoplist',$this->getshop($domainid));
				$this->assign('liveinfo',$res);
				$this->display();
			}
		}else{
			$this->blockID('直播项目前台，参数值：'.$liveID);
			$this->alertmessage('大侠，请按套路出牌！账号已被记录,超过3次将封锁账号，永不解封！','/Live');
			//$this->error('非法访问！IP被记录','',1);
		}
	}

	//单独获取视频流地址
	public function getVideoaddres(){
		$liveid = I('post.liveid');
		$money = I('post.money');
		$dirll = '-';
		if($this->checkTicket($liveid,$money,$dirll)){
			$liveing = D(C('TABLE_LIVEING'));
			$res = $liveing->field('live_addres')->find($liveid);
			if($res){
				echo $res['live_addres'];
			}
		}else{
			echo 0;//票都没买就看不成
		}

	}

	/*
	 * 获取商品
	 */
	public function getshop($domainId){
		//echo "<video src=\"http://pcloud.shixiutv.com/hls/2751/1um7b4i/p11/index.m3u8\" height=\"300\" width=\"400\"></video>";
		//$r = request_get(C('APIADDRES2')."/GetproductInfosByDomainId?domainId=$domainId");
		$r = S('live_shop_'.$domainId);
		if(!$r){
			$r = request_get(C('APIADDRES2')."/GetproductInfosByDomainId?domainId=$domainId");
			S('live_shop_'.$domainId,$r,86400);
		}
		return $r['data'];
		//print_r($r['data']);
	}

	//判断是否购买了直播门票
	protected function checkPaytoTicket($liveID,$money,$dirll){
		if(!empty($liveID)){
			$res = $this->checkTicket($liveID,$money,$dirll);
			if($res){
				//已购买
				//return true;
				$this->assign('yespay',1);
			}else{
				//未购买
				//return false;
				$this->assign('yespay',0);
			}
		}else{
			//return false;
			$this->assign('yespay',0);
		}
	}
	//直接获取是否购买过票
	protected function checkTicket($liveID,$money,$dirll=null){
		if(!$money){
			$money = 0;
		}
		$userinfo = $_COOKIE['user_userinfo'];
		if(empty($dirll) || $dirll==0) {
			$where = "live_id=$liveID and user_token='" . $userinfo . "' and protype='liveitem' and consume_moneynum=$money";
		}else if(empty($money) || $money==0){
			$where = "live_id=$liveID and user_token='" . $userinfo . "' and protype='liveitem' and consume_dirllnum=$dirll";
		}else{
			$where = "live_id=$liveID and user_token='" . $userinfo . "' and protype='liveitem'";
		}
		$moeyc = D(C('TABLE_MONEYCONSUME'));
		$res = $moeyc->where($where)->select();

		return $res;
	}

	//判断直播的时间状态
	protected function checkLiveTimeStatus($livestarttime,$liveendtime){
		$time = strtotime(date("Y-m-d H:i:m"));
		if($time-strtotime($livestarttime)<0){
			//直播未开始
			$res = 0;
			//$this->assign('timestatus',0);
		}else if($time-strtotime($liveendtime)>0){
			//直播已结束
			$res = 2;
			//$this->assign('timestatus',2);
		}else if($time-strtotime($livestarttime)>=0 && $time-strtotime($liveendtime)<=0){
			//正在直播
			$res = 1;
			//$this->assign('timestatus',1);
		}else{
			//其他状态
			$res = -1;
			//$this->assign('timestatus',-1);
		}
		$this->assign('timestatus',$res);
	}

	//判断直播是免费还是收费
	protected function checkLiveMoneyStatus($livePre,$livedirll){
		if(($livePre==0 || empty($livePre)) and ($livedirll==0 || empty($livedirll))){
			//免费
			$this->assign('moneystatus',0);
		}else{
			//收费
			$this->assign('moneystatus',1);
		}
	}

	/*
	 * 自动过滤已结束或正在进行的直播提醒设置
	 */
	protected function autoFiltrateRemind($liveID,$livestarttime,$liveendtime){
		$getTime = date("Y-m-d H:i:m");
		$remind = D(C('TABLE_REMIND'));
		$res = $remind->where(array('live_id'=>$liveID))->select();
		if($res){
			//满足条件则删除
			if($getTime>=$livestarttime || $getTime>=$liveendtime){
				$this->deleteliveRemind($liveID);
			}
		}
	}

	//执行删除过期的直播或正在直播的提醒
	protected function deleteliveRemind($liveID){
		$remind = D(C('TABLE_REMIND'));
		$res = $remind->where(array('live_id'=>$liveID))->delete();
		return $res;
	}

	//判断是否设置过提醒，根据用户Token和直播ID
	public function checkUsertokenToRemind($liveID){
		$remind = D(C('TABLE_REMIND'));
		$res = $remind->where(array('live_id'=>$liveID,'usertoken'=>userCookie()))->select();
		if($res){
			$this->assign('yeset',1);
			//echo 1;//此cookie已经设置过本直播的提醒
		}
	}

	/*
	 * 收藏与取消收藏
	 */
	public function iscollect(){
		$userinfo = $_COOKIE['user_userinfo'];
		$liveid = I('post.liveid');
		$t = I('post.t');
		if($t==1) {
			//取消收藏
			$this->chanelfarorite($userinfo, $liveid);
		}else{
			//收藏
			$this->insertintofarorite($userinfo,$liveid);
		}
	}

	//取消收藏
	protected function chanelfarorite($userinfo,$liveid){
		$favorite = D(C('TABLE_FAVORITE'));
		$res = $favorite->where(array('user_token'=>$userinfo,'live_id'=>$liveid))->delete();
		return $res;
	}
	//收藏
	protected function insertintofarorite($userinfo,$liveid){
		$favorite = D(C('TABLE_FAVORITE'));
		$favorite->user_token = $userinfo;
		$favorite->live_id = $liveid;
		$favorite->favorite_time = date("Y-m-d H:i:m");
		$res = $favorite->add();
		return $res;
	}

	/*
	 * 充值换钻页面
	 */
	public function swopdrill(){
		$liveid = I('get.liveid');
		if(!empty($liveid)){
			$liveing = D(C('TABLE_LIVEING'));
			$res = $liveing->find($liveid);
			if($res){
				$this->assign('drillnum',$this->getdrill());
				$this->assign('liveinfo',$res);
				$this->display();
			}
		}
	}

	/*
	 * 送礼物版块
	 */
	protected function giveGift(){
		$this->assign('gift',gift());
	}

	/*
	 * 判断个人账户是否有足够的钻石
	 */
	public function isdrill(){
		if($this->getdrill() < 1){
			echo 0;
		}else if($this->getdrill() < 10){
			echo 2;
		}else{
			echo 1;
		}
		//dump($r);die;
		//echo 0;//没有钻石
		//echo 1;//有钻石
		//echo 2;//钻石数量少于10个
	}
	//获取钻石数量
	protected function getdrill(){
		$r = request_get(C('APIADDRES1')."/userscore?token=".$_COOKIE['user_userinfo']);
		if($r['ret']==1){
			return $r['data']['point'];
		}else{
			return 00;
		}
	}

	/*
	 * 华人钻消费
	 */
	public function drillconsume(){
		$userinfo = $_COOKIE['user_userinfo'];
		$givev = I('post.givev');
		$bodystr = I('post.bodystr');
		$r = request_get(C('APIADDRES1')."/userscore?token=".$_COOKIE['user_userinfo']);
		if($r['ret']==1){
			$num = $r['data']['point'];
			if($num<$givev){
				echo 0;//华人钻数量不够
			}else{
				$r = request_get(C('APIADDRES1')."/costscore?token=".$userinfo."&score=".$givev."&msg=".urlencode($bodystr));
				if($r['ret']==1){
					if($r['data']['msg']=='ok'){
						echo 1;	//成功
					}
				}
			}
		}
	}

	/*
	 * 收藏
	 */
	public function favorite(){
		$userinfo = $_COOKIE['user_userinfo'];
		$favorite = D(C('TABLE_FAVORITE'));
		$res = $favorite->where(array('user_token'=>$userinfo))->select();
		if($res){
			$id = null;
			foreach($res as $v){
				$id .= $v['live_id'].",";
			}
			$id = rtrim($id,',');
			$liveing = D(C('TABLE_LIVEING'));
			$map['id']  = array('in',$id);
			$res1 = $liveing->where($map)->select();
			$this->assign('livelist',$res1);
			$this->display();
		}
	}
	//取消
	public function cancelfavorite(){
		$userinfo = $_COOKIE['user_userinfo'];
		$liveid = I('get.liveid');
		if($this->chanelfarorite($userinfo,$liveid)){
			header('Location:/Live/Index/favorite');
		}
	}



//	public function area(){
//		print_r(iptoarea());
//	}
	/*
	 * 访问量信息统计
	 */
	public function tj($liveid){
		$uvinfo = $_COOKIE['uvinfo'];
		if(empty($uvinfo)){
			setcookie('uvinfo','l!ive@hr&cn');
		}
		$tjinfo = iptoarea();
		//$liveid = I('post.liveid');
		if($this->tjinfo($liveid,$tjinfo)){
			if($this->checktjnumtoliveid($liveid)){
				if(empty($uvinfo)){
					$this->updatetjunm($liveid,1);
				}else{
					$this->updatetjunm($liveid,2);
				}
			}else{
				$this->inserttjunm($liveid);
			}
		}else{
			if($this->inserttjinfo($liveid,$tjinfo,$uvinfo)){
				if($this->checktjnumtoliveid($liveid)){
					$this->updatetjunm($liveid,3);
				}else{
					$this->inserttjunm($liveid);
				}
			}
		}

	}
	//更新tjnum
	protected function updatetjunm($liveid,$type){
		$tjnum = D(C('TABLE_TJNUM'));
		$where['live_id'] = $liveid;
		if($type==1){								//ip一样，更新pv、uv
			$tjnum->where($where)->setInc('pv_num', 1);
			$tjnum->where($where)->setInc('uv_num', 1);
		}else if($type==2){							//uv一样，更新pv
			$tjnum->where($where)->setInc('pv_num', 1);
		}else{										//都不存在，更新pv、uv、ip
			$tjnum->where($where)->setInc('ip_num', 1);
			$tjnum->where($where)->setInc('pv_num', 1);
			$tjnum->where($where)->setInc('uv_num', 1);
		}
		$res = $tjnum->where($where)->save();
		if($res){
			return true;
		}else{
			return false;
		}
	}
	//判断访问者信息是否存在
	protected function tjinfo($liveid,$tjinfo){
		$ip = $tjinfo['ip'];
		$tj = D(C('TABLE_TJ'));
		$res = $tj->where(array('live_id'=>$liveid,'ip'=>$ip))->select();
		if($res){
			return true;
		}else{
			return false;
		}
	}
	//添加访问者信息
	protected function inserttjinfo($liveid,$tjinfo,$uvinfo){
		$tj = D(C('TABLE_TJ'));
		$data['live_id'] = $liveid;
		$data['country'] = $tjinfo['country'];
		$data['area'] = $tjinfo['area'];
		$data['region'] = $tjinfo['region'];
		$data['city'] = $tjinfo['city'];
		$data['county'] = $tjinfo['county'];
		$data['isp'] = $tjinfo['isp'];
		$data['ip'] = $tjinfo['ip'];
		$data['visitdate'] = date("Y-m-d H:i:m");
		//$data['uvinfo'] = $uvinfo;
		$res = $tj->add($data);
		if($res){
			return true;
		}else{
			return false;
		}
	}
	//新增tjnum
	protected function inserttjunm($liveid){
		$tjnum = D(C('TABLE_TJNUM'));
		$tjnum->live_id = $liveid;
		$tjnum->visitdate = date("Y-m-d H:i:m");
		$res = $tjnum->add();
		if($res){
			return true;
		}else{
			return false;
		}
	}
	//判断tjnum是否存在liveid
	protected function checktjnumtoliveid($liveid){
		$tjnum = D(C('TABLE_TJNUM'));
		$res = $tjnum->where(array('live_id'=>$liveid))->select();
		if($res){
			return true;
		}else{
			return false;
		}
	}

	/*
	 * 支付华人钻看直播
	 */
	public function undirll(){
		$geturl 	=	I('post.url');
		$getdirll 	=	I('post.dirll');
		$liveid 	=	I('post.liveid');
		$bodystr 	=	I('post.bodystr');

		$userinfo = $_COOKIE['user_userinfo'];
		$r = request_get(C('APIADDRES1')."/userscore?token=".$userinfo);
		if($r['ret']==1){
			$num = $r['data']['point'];
			if($num<$getdirll){
				echo 0;//华人钻数量不够
			}else{
				$mconsume = D(C('TABLE_MONEYCONSUME'));
				$res = $mconsume->where(array('live_id'=>$liveid,'user_token'=>$userinfo))->count();
				if($res>0){
					echo 1; //成功
				}else{
					//扣钻
					$r = request_get(C('APIADDRES1')."/costscore?token=".$userinfo."&score=".$getdirll."&msg=".urlencode($bodystr));
					if($r['ret']==1){
						if($r['data']['msg']=='ok'){
							//echo 1;	//成功
							$data['user_token'] = $userinfo;
							$data['live_id'] = $liveid;
							$data['consume_dirllnum'] = $getdirll;
							$data['consume_remark'] = $bodystr;
							$data['consume_time'] = date("Y-m-d H:i:m");
							$data['protype'] = 'liveitem';
							$res = $mconsume->add($data);
							if($res){
								echo 1;
							}
						}
					}else{
						echo 2;//支付失败
					}

				}

			}
		}

	}


}